/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.hiof.moraa.server.service;

import com.hiof.moraa.mapping.TblAthlete;
import com.hiof.moraa.mapping.TblSupport;
import com.hiof.moraa.server.dao.*;
import com.hiof.moraa.server.service.model.Feed;
import com.hiof.moraa.server.service.model.FeedTimeStampComparator;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author Bergy
 */
@Transactional(propagation = Propagation.REQUIRED)
public class FeedService {

    private static int numOfLiveLocationFeed = 10;
    private static int numOfLiveMediaFeed = 10;
    private static int numOfPassTargetFeed = 10;
    AthleteLiveLocationDAO athleteLiveLocationDAO;
    SupportLiveLocationDAO supportLiveLocationDAO;
    AthleteLiveMediaDAO athleteLiveMediaDAO;
    SupportLiveMediaDAO supportLiveMediaDAO;
    PassTargetDAO passTargetDAO;
    FeedTimeStampComparator comparator;
    private AthleteDAO athleteDAO;

    public void setAthleteLiveMediaDAO(AthleteLiveMediaDAO athleteLiveMediaDAO) {
        this.athleteLiveMediaDAO = athleteLiveMediaDAO;
    }

    public void setPassTargetDAO(PassTargetDAO passTargetDAO) {
        this.passTargetDAO = passTargetDAO;
    }

    public void setSupportLiveMediaDAO(SupportLiveMediaDAO supportLiveMediaDAO) {
        this.supportLiveMediaDAO = supportLiveMediaDAO;
    }

    public void setAthleteLiveLocationDAO(AthleteLiveLocationDAO athleteLiveLocationDAO) {
        this.athleteLiveLocationDAO = athleteLiveLocationDAO;
    }

    public void setSupportLiveLocationDAO(SupportLiveLocationDAO supportLiveLocationDAO) {
        this.supportLiveLocationDAO = supportLiveLocationDAO;
    }

    public AthleteDAO getAthleteDAO() {
        return athleteDAO;
    }

    public void setAthleteDAO(AthleteDAO athleteDAO) {
        this.athleteDAO = athleteDAO;
    }

    public void setComparator(FeedTimeStampComparator comparator) {
        this.comparator = comparator;
    }

    public List<Feed> getLatestFeedsOfOneRoute(int routeId) {
        List<Feed> result = new ArrayList<Feed>();
        result.addAll(athleteLiveMediaDAO.getLatestLiveMediaFeedOfAllAthletesInOneRoute(routeId, numOfLiveMediaFeed));
        result.addAll(supportLiveMediaDAO.getLatestLiveMediaFeedOfAllSupportsInOneRoute(routeId, numOfLiveMediaFeed));
        result.addAll(passTargetDAO.getLatestPassTargetFeedOfAllAthletesInOneRoute(routeId, numOfPassTargetFeed));
        comparator.setIsReverse(true);
        Collections.sort(result, comparator);
        return result;
    }

    public List<Feed> getLatestFeedsOfAthleteInOneRouteByUsername(String username, int routeId) {
        List<Feed> result = new ArrayList<Feed>();
        result.addAll(athleteLiveMediaDAO.getLatestLiveMediaFeedOfOneAthleteInOneRoute(username, routeId, numOfLiveMediaFeed));
        result.addAll(passTargetDAO.getLatestPassTargetFeedOfOneAthleteInOneRoute(username, routeId, numOfPassTargetFeed));

        TblAthlete athlete = athleteDAO.getAthleteByUsernameAndRouteId(username, routeId);
        List<TblSupport> supports = athleteDAO.getSupports(athlete.getId());
        for (TblSupport support : supports) {
            String supportUsername = support.getTblUser().getUsername();
            result.addAll(supportLiveMediaDAO.getLatestLiveMediaFeedOfOneSupportInOneRoute(supportUsername, routeId, numOfLiveMediaFeed));
        }

        comparator.setIsReverse(true);
        Collections.sort(result, comparator);
        return result;
    }
}
